package com.china08.yxyapi.api.manage.sch.platform.statistics.sws.swsschool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsPvForDailyAnalyze;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsPvForDailyAnalyzeRepository;

@CrossOrigin
@RestController
public class SwsSchoolStatictisTrendController extends YxyApiBaseController{
    
    @Autowired
    private SwsPvForDailyAnalyzeRepository  swsPvForDailyAnalyzeRepository;
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/sch/platform/statistics/sws/swsschool/trend/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel req) {
       RespModel model = new RespModel();
       String schoolId = getProfile(request, response).getSchoolId();
       
       ArrayList<Integer> oldDateList = null; 
       if (req.dateValue.equals(7) || req.dateValue == 7) {
           oldDateList = SwsSchoolStatictisTrendController.getOldDate(7);
           String oldDatestr = null;
           SwsPvForDailyAnalyze swsPvForDailyAnalyze = null;
           List<Long> pvWeekList = new ArrayList<Long>();
           for (Integer oldDate : oldDateList) {
               oldDatestr = oldDate.toString();
               swsPvForDailyAnalyze = swsPvForDailyAnalyzeRepository.findBySchoolIdAndYearForPvAndMonthForPvAndDayForPv(schoolId, Integer.valueOf(oldDatestr.substring(0, 4)), Integer.valueOf(oldDatestr.substring(4, 6)), Integer.valueOf(oldDatestr.substring(6)));
               pvWeekList.add(swsPvForDailyAnalyze.getPv());
           }
           model.pvWeekList = pvWeekList;
       }else if (req.dateValue.equals(30) || req.dateValue == 30) {
           oldDateList = SwsSchoolStatictisTrendController.getOldDate(30);
           String oldDatestr = null;
           SwsPvForDailyAnalyze swsPvForDailyAnalyze = null;
           List<Long> pvMonthList = new ArrayList<Long>();
           for (Integer oldDate : oldDateList) {
               oldDatestr = oldDate.toString();
               swsPvForDailyAnalyze = swsPvForDailyAnalyzeRepository.findBySchoolIdAndYearForPvAndMonthForPvAndDayForPv(schoolId, Integer.valueOf(oldDatestr.substring(0, 4)), Integer.valueOf(oldDatestr.substring(4, 6)), Integer.valueOf(oldDatestr.substring(6)));
               pvMonthList.add(swsPvForDailyAnalyze.getPv());
           }
           model.pvMonthList = pvMonthList;
       }
       return model;   
    }
    protected static class RespModel extends YxyApiRespModel {
        //一周
        public List<Long> pvWeekList;
        //一个月
        public List<Long> pvMonthList;
    }
    protected static class ReqModel extends YxyApiReqModel {
        // 一周或者是一个月
        public Integer dateValue;
    }
    // 获取过去N天的时间
    public static ArrayList<Integer> getOldDate(int intervals) {
        ArrayList<Integer> pastDaysList = new ArrayList<>();
        for (int i = intervals-1; i >= 0 ; i--) {
            pastDaysList.add(Integer.parseInt(getPastDate(i)));
        }
        return pastDaysList;
    }

    // 获取过去N天的时间
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String result = format.format(today);
        return result;
    }
}
